package indi.readbook.example.common.utils.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Generator {
	public static void main(String[] args) {
		String packageName = "com.yijiaganxi.order";
		boolean serviceNameStartWithI = false;// auth -> UserService, 设置成true: auth -> IUserService
		generateByTables(serviceNameStartWithI, packageName, "zhangguowei", "yijiaganxi","expand_business");

		System.out.println("completed...");
	}

	/**
	 * @param serviceNameStartWithI
	 * @param packageName           包名
	 * @param author                作者
	 * @param database              数据库名
	 * @param tableNames            表名
	 */
	private static void generateByTables(boolean serviceNameStartWithI, String packageName, String author,
			String database, String... tableNames) {
		GlobalConfig config = new GlobalConfig();
		String dbUrl = "jdbc:mysql://127.0.0.1:3306/" + database
				+ "?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL";
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL).setUrl(dbUrl).setUsername("root").setPassword("123456")
				.setDriverName("com.mysql.cj.jdbc.Driver");
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setCapitalMode(true).setEntityLombokModel(false).setDbColumnUnderline(true)
				.setNaming(NamingStrategy.underline_to_camel)
//				.setSuperMapperClass("cn.saytime.mapper.BaseMapper")
				.setInclude(tableNames);// 修改替换成你需要的表名，多个表名传数组
		config.setActiveRecord(false).setAuthor(author).setOutputDir("G:\\codeGen").setFileOverride(true)
				.setEnableCache(false);
		if (!serviceNameStartWithI) {
			config.setServiceName("%sService");
		}
		new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig)
				.setPackageInfo(new PackageConfig().setParent(packageName).setController("controller.shop").setEntity("entity.shop")
						.setMapper("dao.shop").setService("service.shop").setServiceImpl("service.impl.shop")
						.setXml("mybatis.mappers"))
				.execute();
	}

}
